﻿using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.RightsManagement;
using System.Text;
using System.Threading.Tasks;

namespace DonetArxLib
{

    
    internal class PointPositWriter
    {
        private List<PointData> arr = new List<PointData> ();
        
        public PointPositWriter(string fileName)
        {
            _fileName = fileName;
            _fileCount = 0;
            _lineCount = 0;

            arr.Sort();
        }

        public void WritePointInfo(Point3d pt1, Point3d pt2, Point3d pt3)
        {
            if (_stream == null)
                _stream = new StreamWriter($"{_fileName}{_fileCount}", false, Encoding.UTF8);

            if (_lineCount > 2000000)
            {
                _fileCount++;
                _stream.Flush();
                _stream.Close();
                _stream = new StreamWriter($"{_fileName}{_fileCount}", false, Encoding.UTF8);
                _lineCount = 0;
            }    

            string content = $"{pt1.X.ToString("f6")},{pt1.Y.ToString("f6")},{pt1.Z.ToString("f6")}," +
                $"{pt2.X.ToString("f6")},{pt2.Y.ToString("f6")},{pt2.Z.ToString("f6")}," +
                $"{pt3.X.ToString("f6")},{pt3.Y.ToString("f6")},{pt3.Z.ToString("f6")}";
            _stream.WriteLine(content);
            _lineCount++;
        }

        public void Flush()
        {
            _stream.Flush();
        }

        public void Finish()
        { 
            _stream.Flush();
            _stream.Close();

            _lineCount = 0;
            _fileCount= 0;
        }

        private StreamWriter _stream;
        private int _lineCount;
        private int _fileCount;
        private string _fileName;

    }
}
